package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.SkuProperty;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/skuProperty")
public class SkuPropertyController {

    @Reference
    private ICatalogService catalogService;
    @Reference
    private ISkuPropertyService skuPropertyService;

    /**
     *显示sku商品分类,从redis缓存中拿
     * @param model
     * @return
     */
    @RequestMapping("")
    public String skuPropertyPage(Model model){
        model.addAttribute("allCatalog",catalogService.selectAllByCache());
        return "skuProperty/skuProperty";
    }

    /**
     * 显示类别下的分类属性
     * @param catalogId
     * @param model
     * @return
     */
    @RequestMapping("/get/{id}")
    public String get(@PathVariable("id") Long catalogId, Model model){

        model.addAttribute("skuPropertyList",skuPropertyService.queryByCId(catalogId));
        return "skuProperty/skuProperty_list";
    }


    /**
     * 新增或编辑分类属性
     * @param skuProperty
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JSONResultVo saveOrUpdate(SkuProperty skuProperty){
        skuPropertyService.saveOrUpdate(skuProperty);
        return JSONResultVo.instance();
    }


    @RequestMapping("/delete/{id}")
    @ResponseBody
    public JSONResultVo delete(@PathVariable("id") Long skuPropertyId){

        skuPropertyService.deleteByPId(skuPropertyId);
        return JSONResultVo.instance();
    }


}
